# 商品信息
goods = [
    {'name': '棒棒糖', 'price': 1.50, 'count': 500},
    {'name': '香瓜子', 'price': 6.00, 'count': 100},
    {'name': '麻辣王子', 'price': 5.00, 'count': 200},
    {'name': '气球', 'price': 2.00, 'count': 500},
    {'name': '苹果', 'price': 9.9, 'count': 200}
         ]

goods_car = []


# 展示商品信息
def show_goods_msg():
    i = 1
    print('start', '-'*100)
    for temp in goods:
        if temp.get('count') <= 0:
            continue
        print('%03d\t商品名称：%-15s\t价格：%-10.2f\t库存：%-5d' % (i, temp.get('name'), temp.get('price'), temp.get('count')))
        i += 1
    print(' end', '-' * 100)


# 购买商品
def buy_goods():
    print('*'*50)
    good_id = int(input('请输入商品序号：'))
    good_count = int(input('您需要购买多少件：'))
    if good_id > len(goods):
        print('没有此件商品，请重新输入')
        buy_goods()
    else:
        temp = goods[good_id-1]
        if good_count > temp.get('count'):
            print('库存不足，请重新输入')
            buy_goods()
        else:
            print('订单：\n商品：%s\n数量：%d\n总价：%.2f' %(temp.get('name'), good_count, good_count*temp.get('price')))
            status = input("确认订单：‘y’， 取消订单：‘其他任意键’，加至购物车：‘add’")
            if status.lower() == 'y':
                goods[good_id-1]['count'] -= good_count
                print('购买成功')
            elif status.lower() == 'add':
                goods_car.append(dict(temp))
                goods_car[-1]['count'] = good_count
                print('已加至购物车')
            else:
                print('已取消此订单！！！')
            print('*' * 50)


# 查看购物车
def show_goods_car():
    i = 1
    total_price = 0
    print('start', '-'*100)
    for temp in goods_car:
        if temp.get('count') <= 0:
            continue
        total_price += temp.get('price')*temp.get('count')
        print('%03d\t商品名称：%-15s\t单价：%-10.2f\t数量：%-5d\t总价：%-10.2f' %
              (i, temp.get('name'), temp.get('price'), temp.get('count'), temp.get('price')*temp.get('count')))
        i += 1
    print(f'￥：{total_price}元')
    print(' end', '-' * 100)


# 清空购物车
def clear_car():
    goods_car.clear()
    print('购物车已清空')


# 结算购物车
def pay_for_car():
    for i in goods_car:
        for j in goods:
            if i.get('name') == j.get('name'):
                j['count'] -= i['count']
                continue
    goods_car.clear()
    print('已结算')


# 操作指南
def guide():
    print('start', '=' * 100)
    print('操作指南')
    print('%-10s%-10s' % ('打印指南', 'guide'))
    print('%-10s%-10s' % ('展示所有商品', 'show'))
    print('%-10s%-10s' % ('购买商品', 'buy'))
    print('%-10s%-10s' % ('查看购物车', 'car'))
    print('%-10s%-10s' % ('清空购物车', 'clear'))
    print('%-10s%-10s' % ('结算购物车', 'pay'))
    print('%-10s%-10s' % ('退出系统', 'exit'))
    print(' end', '=' * 100)


if __name__ == '__main__':
    guide()
    while True:
        cmd = input('请输入命令：')
        if cmd.lower() == 'show':
            show_goods_msg()
        elif cmd.lower() == 'buy':
            buy_goods()
        elif cmd.lower() == 'guide':
            guide()
        elif cmd.lower() == 'car':
            show_goods_car()
        elif cmd.lower() == 'clear':
            clear_car()
        elif cmd.lower() == 'pay':
            pay_for_car()
        elif cmd.lower() == 'exit':
            break
        else:
            print('命令输入错误')



